package com.edu.dao;

import com.edu.bo.PaperGengerateVO;
import com.edu.entity.Paper;
import com.edu.entity.PaperQuestion;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface PaperMapper {
    /*
        1.保存试卷
        a.statement: savePaper [Java配置]
        b.目标表: exam_paper
        b.入参: Paper
        c.返回: int
    */
    String FIELDS = "name,status,startTime,endTime,levelId,updateDate,createDate";
    String VALUES = "#{name},#{status},#{startTime},#{endTime},#{levelId},now(),now()";
    @Insert("INSERT INTO exam_paper("+ FIELDS +") VALUES("+ VALUES +")")
    @Options(keyProperty="id",keyColumn="id",useGeneratedKeys=true)
    int savePaper(Paper paper);

    /*
        2.更新试卷
         a.statement: updatePaper [Java配置]
         b.目标表: exam_paper
         c.入参: Paper
         d.返回: int
    */
    String UPDATE_FIELDS = "name=#{name},status=#{status},startTime=#{startTime},"+
                          "endTime=#{endTime},levelId=#{levelId},updateDate=now()";
    @Update("update exam_paper set "+ UPDATE_FIELDS +" where id=#{id}")
    int updatePaper(Paper paper);


    /*
        3. 查询试卷列表
          a.statement: queryList [XML配置]
          b.左联接:   t_dictype_data
          c.联接条件: P.levelId = id
          d.模糊搜索: id, name
          e.List<Paper>
    */
    List<Paper> queryList(Map map);

    /*
        查询全部的试卷列表
        5.statement: queryPaper [Java配置]
        a.目标表: exam_paper
        b.条件: 无
        c.返回: List<Paper>
     */
    @Select("select * from exam_paper")
    List<Paper> queryPaper();

    /*
        根据试卷查询问题列表
        6.statement: queryQuestionByPaperId
              [Java配置]
        a.目标表: exam_paper_question
        b.条件: paperId (试卷 ID)
        c.返回: List<PaperQuestion>
    */
    @Select("select * from exam_paper_question where paperId=#{paperId}")
    List<PaperQuestion> queryQuestionByPaperId(Integer paperId);


    /*
    	7.statement: insertBatchPaperQuestion
          a.动作: 多行插入 [paperId,questionId]
          b.目标表: exam_paper_question
          c.使用脚本标签(动态 SQL).
            [paperId], [questionId]
          d.List<Map> params
          e.返回: int 类型
    */
    @Insert(
            "<script>"+
                    "insert into exam_paper_question (paperId,questionId)"+
                    "values"+
                    "<foreach collection='params' item='data' separator=','>"+
                         "(#{data.paperId}, #{data.questionId})"+
                    "</foreach>"+
            "</script>"
    )
    int insertBatchPaperQuestion(
            @Param("params") List<Map> params);


    /*
        8.删除试卷的问题。
        ps: 要求根据试卷id来删除
        操作表: [exam_paper_question]
     */
    @Delete("delete from exam_paper_question where paperId=#{paperId}")
    int deletePaperQuestion(Integer paperId);

    @Delete("delete from exam_paper where id=#{id}")
    int deleteNoPaperQuestion(Integer paperId);
	

    PaperGengerateVO previewPaper(Integer paperId);


    List<Paper> queryAll();

}
